﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Company;
using Frame.Lib.Common;

namespace Cms.Admin.Company
{
    public partial class List : PageList
    {
        private CompanyBO Company
        {
            get
            {
                return CompanyBO.Instance<CompanyBO>();
            }
        }

        private string PageSize
        {
            get
            {
                return Request.QueryString["PageSize"];
            }
        }

        private string DomainName
        {
            get
            {
                return Request.QueryString["DomainName"];
            }
        }

       // private PagerSortSetting _setting;
        protected void Page_Load(object sender, EventArgs e)
        {
          //  _setting = this.Footer1.InitPager(new BindViewHandler(BindView));
            if (!IsPostBack)
            {
                Pager.BindDrp(drpPager);

                if (!string.IsNullOrEmpty(this.PageSize))
                {
                    drpPager.SelectedValue = this.PageSize;
                }

                txtSearch.Text = this.DomainName;

                BindView();

            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            JSUtil.SetTableHeight("list", 145);
        }

        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "公司");

            string url = "Edit.aspx";

            JSUtil.ShowForm(lnkAdd, "增加公司", null, GlobalVar.WebCurrPath + url, 480, 531);
            JSUtil.ClickBtn(lnkDelete, btnDelete);

        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);

            chkSelectAll.Checked = false;

            //_setting.Input = txtSearch.Text.Trim();
            //Pager.SetPageSize(_setting, drpPager);

            PagerSetting setting = new PagerSetting();
            setting.PageSize = int.Parse(drpPager.SelectedValue);
            setting.PageIndex = this.PageIndex;
            if (txtSearch.Text.Trim() != "")
            {
                setting.Where = "SiteDomain like @p0";
            }


            IEnumerable<CompanyInfo> list = this.Company.GetList(setting, "%" + txtSearch.Text.Trim() + "%");

            lblStatistic.Text = "[&nbsp;共<b> " + setting.RecordCount + "</b>&nbsp;个公司&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();

            string prefix = GlobalVar.WebCurrPath + "List.aspx?PageSize=" + drpPager.SelectedValue + "&DomainName=" + txtSearch.Text.Trim();

            litPager.Text = HtmlPager.Render(setting, prefix, true);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            IEnumerable<int> ids = StrUtil.SplitToInt(strSelectIDs);           
          
            this.Company.Delete(ids);
            BindView();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 480, 531);
            }
        }

    }
}